//
//  StretchableTableHeaderView.m
//  StretchableTableHeaderView
//

#import "HFStretchableTableHeaderView.h"

@interface HFStretchableTableHeaderView()<UIScrollViewDelegate>
{
    CGRect initialFrame;
    CGFloat defaultViewHeight;
}
@end


@implementation HFStretchableTableHeaderView



- (void)stretchHeaderForTableView:(UITableView*)tableView withView:(UIView*)view
{
    _tableView = tableView;
    _view = view;
    
    initialFrame       = _view.frame;
    defaultViewHeight  = initialFrame.size.height;
    
    UIView* emptyTableHeaderView = [[UIView alloc] initWithFrame:initialFrame];
    _tableView.tableHeaderView = emptyTableHeaderView;
    
    [_tableView addSubview:_view];
     _maskingView= [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    
    _maskingView.alpha = 1;

    
    [_view addSubview:_maskingView];
    
    
    
//    self.backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)];
//    
//    self.backgroundView.image = [UIImage imageNamed:@"bg_albumView_header@2x.png"];
//
//    [self.view addSubview:self.backgroundView];
    
    
    
    
    self.photoBackImage = [[UIImageView alloc]initWithFrame:CGRectMake(30, 50, 90, 90)];
    //        self.photoBackImage.backgroundColor = [UIColor cyanColor];
    [self.photoBackImage setImage:[UIImage imageNamed:@"album_cover_bg@2x.png"]];
    [self.maskingView addSubview:_photoBackImage];
    
    
    self.photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 70, 70)];
//    self.photoImage.backgroundColor = [UIColor cyanColor];
    [self.photoBackImage addSubview:_photoImage];
    
    self.smallButton = [[UIButton alloc]initWithFrame:CGRectMake(130,60, 30, 30)];
    [self.maskingView addSubview:self.smallButton];
    
    self.nameButton = [[UIButton alloc]initWithFrame:CGRectMake(165, 60, 50, 30)];
    [self.view addSubview:self.nameButton];
    
    self.creatLabel = [[UILabel alloc]initWithFrame:CGRectMake(230, 60, 30, 30)];
    self.creatLabel.font = [UIFont systemFontOfSize:13];
    self.creatLabel.text = @"创建";
    [self.maskingView addSubview:self.creatLabel];
    
    self.countImage = [[UIImageView alloc]initWithFrame:CGRectMake(260, 65, 20, 20)];
    self.countImage.image = [UIImage imageNamed:@"albumInfoCell_tracks_mark@2x.png"];
    [self.maskingView addSubview:_countImage];
    
    self.countLable = [[UILabel alloc]initWithFrame:CGRectMake(285, 65, 20, 20)];
//    self.countLable.backgroundColor = [UIColor cyanColor];
    [self.maskingView addSubview:self.countLable];
    
    
    
    
    self.personNameButton = [[UIButton alloc]initWithFrame:CGRectMake(130, 110, 80, 30)];
    [_personNameButton setTitle:@"访问主播" forState:UIControlStateNormal];
    _personNameButton.titleLabel.font= [UIFont systemFontOfSize:13];
    [self.maskingView addSubview:self.personNameButton];
    
    
    self.orderButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 110, 80, 30)];
    [self.orderButton setTitle:@"订阅专辑" forState:UIControlStateNormal];
    _orderButton.titleLabel.font= [UIFont systemFontOfSize:13];
    [self.maskingView addSubview:self.orderButton];
    
    
    

    
    
    
    
    
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    CGRect f = _view.frame;
    f.size.width = _tableView.frame.size.width;
    _view.frame = f;
    
    if(scrollView.contentOffset.y < 0) {
        CGFloat offsetY = (scrollView.contentOffset.y + scrollView.contentInset.top) * -1;
        initialFrame.origin.y = offsetY * -1;
        initialFrame.size.height = defaultViewHeight + offsetY;
        _view.frame = initialFrame;
        _maskingView.frame = CGRectMake(0, 0, _view.frame.size.width, _view.frame.size.height);
        
        
        
        
        self.photoBackImage.frame = CGRectMake(30,initialFrame.size.height -  100, 90, 90);
        
      
        self.smallButton.frame = CGRectMake(130,initialFrame.size.height -  90, 30, 30);
        self.nameButton.frame = CGRectMake(165, initialFrame.size.height -  90, 50, 30);
        
        self.creatLabel.frame = CGRectMake(230, initialFrame.size.height -  90, 30, 30);
        self.countImage.frame = CGRectMake(260,initialFrame.size.height -  85 , 20, 20);
        self.countLable.frame = CGRectMake(285,initialFrame.size.height -  85, 20, 20);
        
        
        
        self.personNameButton.frame =CGRectMake(130,initialFrame.size.height -  40, 80, 30);
        self.orderButton.frame =CGRectMake(200, initialFrame.size.height -  40, 80, 30);
        
    }
}



-(void)setModel:(DynamicIFOAlbumModel *)model
{
    [self.photoImage sd_setImageWithURL:[NSURL URLWithString:model.coverLarge] placeholderImage:[UIImage imageNamed:@"123.gif"]];
    [self.nameButton setTitle:model.title forState:UIControlStateNormal];
    self.nameButton.titleLabel.font = [UIFont systemFontOfSize:13];
    
    self.smallButton.layer.cornerRadius = 15;
    self.smallButton.clipsToBounds = YES;
    [self.smallButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.avatarPath] forState:UIControlStateNormal];
    self.countLable.text = [NSString stringWithFormat:@"%@",model.tracks];
    self.countLable.font = [UIFont systemFontOfSize:13];
    self.countLable.textColor = [UIColor whiteColor];
    

    
}
- (void)resizeView
{
    initialFrame.size.width = _tableView.frame.size.width;
    _view.frame = initialFrame;
    _maskingView.frame = CGRectMake(0, 0, _view.frame.size.width, _view.frame.size.height);
    
}



@end
